// EnhancedMapGenerator.js

class EnhancedMapGenerator {
    constructor() {
        this.mapGroup = new THREE.Group();
        this.validateConfig();
    }

    // 验证配置参数
    validateConfig() {
        const robotDiameterInCells = worldUnitsManager.WORLD.REFERENCE.ROBOT_DIAMETER /
                                   worldUnitsManager.WORLD.GRID.CELL_SIZE_METERS;
        console.log(`Robot diameter covers ${robotDiameterInCells} grid cells`);

        if (Math.abs(robotDiameterInCells - worldUnitsManager.WORLD.REFERENCE.GRID_CELLS_PER_ROBOT) > 0.1) {
            console.warn('Grid cell size might need adjustment');
        }
    }

    // 创建完整的地图模型
    createMapModel() {
        this.addDetailedGrid();
        this.addMajorGrid();
        this.addAxes();
        return this.mapGroup;
    }

    // 添加细网格（5厘米间隔）
    addDetailedGrid() {
        const gridSizeUnits = worldUnitsManager.metersToUnits(worldUnitsManager.WORLD.SIZE_METERS.WIDTH);
        const cellSizeUnits = worldUnitsManager.metersToUnits(worldUnitsManager.WORLD.GRID.CELL_SIZE_METERS);
        const divisions = gridSizeUnits / cellSizeUnits;

        const detailGrid = new THREE.GridHelper(
            gridSizeUnits,
            divisions,
            worldUnitsManager.COLORS.GRID.MINOR,
            worldUnitsManager.COLORS.GRID.MINOR
        );

        detailGrid.material.opacity = worldUnitsManager.WORLD.GRID.MINOR_LINE_OPACITY;
        detailGrid.material.transparent = true;
        detailGrid.position.y = 0;

        this.mapGroup.add(detailGrid);
    }

    // 添加主网格（1米间隔）
    addMajorGrid() {
        const gridSizeUnits = worldUnitsManager.metersToUnits(worldUnitsManager.WORLD.SIZE_METERS.WIDTH);
        const majorIntervalUnits = worldUnitsManager.metersToUnits(worldUnitsManager.WORLD.GRID.MAJOR_INTERVAL);
        const divisions = gridSizeUnits / majorIntervalUnits;

        const majorGrid = new THREE.GridHelper(
            gridSizeUnits,
            divisions,
            worldUnitsManager.COLORS.GRID.MAJOR,
            worldUnitsManager.COLORS.GRID.MAJOR
        );

        majorGrid.material.opacity = worldUnitsManager.WORLD.GRID.MAJOR_LINE_OPACITY;
        majorGrid.material.transparent = true;
        majorGrid.position.y = worldUnitsManager.metersToUnits(worldUnitsManager.WORLD.AXIS.HEIGHT_OFFSET);

        this.mapGroup.add(majorGrid);
    }

    // 添加坐标轴
    addAxes() {
        const axesLength = worldUnitsManager.metersToUnits(worldUnitsManager.WORLD.AXIS.LENGTH_METERS);
        const axesHelper = new THREE.AxesHelper(axesLength);

        // 设置坐标轴位置，稍微抬高以避免与网格重叠
        axesHelper.position.y = worldUnitsManager.metersToUnits(worldUnitsManager.WORLD.AXIS.HEIGHT_OFFSET * 2);

        this.mapGroup.add(axesHelper);
    }

    // Debug helper - 添加参考圆用于验证尺寸
    addRobotSizeReference() {
        const geometry = new THREE.CircleGeometry(
            worldUnitsManager.metersToUnits(worldUnitsManager.WORLD.REFERENCE.ROBOT_DIAMETER / 2),
            32
        );
        const material = new THREE.MeshBasicMaterial({
            color: 0xff0000,
            transparent: true,
            opacity: 0.2
        });
        const circle = new THREE.Mesh(geometry, material);
        circle.rotation.x = -Math.PI / 2;
        circle.position.y = worldUnitsManager.metersToUnits(worldUnitsManager.WORLD.AXIS.HEIGHT_OFFSET * 3);

        this.mapGroup.add(circle);
    }
}

// 导出到全局作用域
if (typeof window !== 'undefined') {
    window.EnhancedMapGenerator = EnhancedMapGenerator;
}

// CommonJS 模块导出
if (typeof module !== 'undefined' && module.exports) {
    module.exports = {
        EnhancedMapGenerator
    };
}